package com.yystar.support.error;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.mozilla.javascript.ErrorReporter;
import org.mozilla.javascript.EvaluatorException;

/**
 * 
 * @author liuzhifei
 *
 */
public class JavaScriptErrorReporter implements ErrorReporter {
    
    private static Log _log = LogFactory.getLog(JavaScriptErrorReporter.class);

    public void error(
            String message, String sourceName, int line, String lineSource,
            int lineOffset) {

            if (line < 0) {
                _log.error(message);
            }
            else {
                _log.error(line + ": " + lineOffset + ": " + message);
            }
        }

        public EvaluatorException runtimeError(
            String message, String sourceName, int line, String lineSource,
            int lineOffset) {

            error(message, sourceName, line, lineSource, lineOffset);

            return new EvaluatorException(message);
        }

        public void warning(
            String message, String sourceName, int line, String lineSource,
            int lineOffset) {

            if (!_log.isWarnEnabled()) {
                return;
            }

            if (line < 0) {
                _log.warn(message);
            }
            else {
                _log.warn(line + ": " + lineOffset + ": " + message);
            }
        }
}
